<?php


namespace app\api\model\dao;

use think\Db;
use think\Model;

class Admin extends Model
{
    //set talbename
    protected $table = 'admin';
    protected $pk = 'id';
    protected $autoWriteTimestamp = true;

    /**
     * @param array $conds
     * @param array $fields
     * @param int $offset
     * @param int $limit
     * @param string $orderby
     * @return mixed
     */
    public function getAdminByConds($conds = [], $fields = [], $offset = 0, $limit = 10000, $orderby = '')
    {
        $this->allowField(true);

        $arrCond = [];
        foreach ($conds as $key => $val) {
            if (in_array($key, $this->field)) {
                $arrCond[$key] = $val;
            }
        }
        empty($arrCond) && $arrCond = '1=1';

        $arrField = [];
        foreach ($fields as $val) {
            if (in_array($val, $this->field)) {
                $arrField[] = $val;
            }
        }
        empty($arrField) && $arrField = $this->field;
        return Db::table($this->table)->where($arrCond)->limit($offset, $limit)->order($orderby)->field($arrField)->select();
    }

    /**
     * @param $data
     * @return false|int
     */
    public function add($data)
    {
        return $this->allowField(true)->save($data);

    }

    /**
     * @param $conds
     * @param $page
     * @return \think\paginator\Collection
     */
    public function userPage($conds=[], $page=10)
    {
        $arrCond = [];
        foreach ($conds as $key => $val) {
            if (in_array($key, $this->field)) {
                $arrCond[$key] = $val;
            }
        }
        empty($page) && $page = 10;
        empty($conds) && $conds = "1=1";
        return $this->where($arrCond)->paginate(10);
    }

}